package com.turning.resume.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.turning.resume.common.Result;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * ClassName: GlobalExceptionHandler
 * Package: com.turning.resume.handler
 * Description:
 *
 * @Author M4N715
 * @Create 2025/9/21 13:50
 * @Version 1.0
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public void handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String errorMsg = e.getBindingResult()
                .getFieldErrors()
                .stream()
                .findFirst()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .orElse("必填字段不能为空");
        log.error("参数校验异常: {}", errorMsg, e);
    }

    @ExceptionHandler(Exception.class)
    public void handleException(Exception e) {
        log.error("全局异常捕获: {}", e.getMessage(), e);
    }
}
